package com.app.newiptv.adaper;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.RecyclerView;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.app.newiptv.R;
import com.app.newiptv.activity.FilmDetailsActivity;
import com.app.newiptv.activity.HomePageActivity;
import com.app.newiptv.activity.SitcomActivity;
import com.app.newiptv.callback.SetFocusCallBack;
import com.app.newiptv.presenter.HomePagePresenter;
import com.app.newiptv.utils.FocusedViewUtils;
import com.bumptech.glide.Glide;


/**
 * Created by wendal on 2017/9/23.
 */

public class HomePageMiddleAdapter extends RecyclerView.Adapter<HomePageMiddleAdapter.MyViewHolder> {
    private HomePagePresenter presenter;
    private int count;
    private View focusview;
    private RecyclerView parentRecycle;
    public View currentView;
    private SetFocusCallBack callBack;
    private Context context;
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

                FocusedViewUtils.setFocusedView(focusview, currentView, 20, 15, 20, 15);

        }
    };
    public HomePageMiddleAdapter(HomePagePresenter presenter, View focusview, SetFocusCallBack callBack, Context context){
        this.presenter = presenter;
        this.focusview = focusview;
        this.callBack = callBack;
        this.context = context;
    }
    public void setCount(int count){
        this.count = count;
    }
    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.home_middle_item,null);
        MyViewHolder viewHolder = new MyViewHolder(view);
        if (parentRecycle == null) {
            parentRecycle = (RecyclerView) parent;
            parentRecycle.addOnScrollListener(new MovieWorldScrollListener());
        }
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        presenter.onBindMiddleData(holder,position);
        holder.setPosition(position);
    }

    @Override
    public int getItemCount() {
        return count;
    }

    class MyViewHolder extends RecyclerView.ViewHolder implements HomePagePresenter.MiddleViewHolder{
        private int position;
        private ImageView imageView;
        public MyViewHolder(final View itemView) {
            super(itemView);
            imageView = (ImageView) itemView.findViewById(R.id.image_item);
            itemView.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                @Override
                public void onFocusChange(View view, boolean b) {
                    if (b) {
                        currentView = view;
                        boolean scroll = FocusedViewUtils.arrowScroll(view);
                        if(!scroll) {
                            FocusedViewUtils.setFocusedView(focusview,view,20, 15, 20, 15);
                        }else{
                            if(handler.hasMessages(0)){
                                handler.removeMessages(0);
                            }
                            handler.sendEmptyMessageDelayed(0,500);
                        }
                    }else{
                        FocusedViewUtils.setBlurView(focusview,view);
                    }
                }
            });
            itemView.setOnKeyListener(new OnKeyListen());
            itemView.setOnClickListener(new SetOnClick());
        }
        public void setPosition(int position){
            this.position = position;
        }
        @Override
        public void setImage(String imageUrl) {
            Glide.with(context).load(imageUrl).into(imageView);
        }
        public class OnKeyListen implements View.OnKeyListener{
            @Override
            public boolean onKey(View view, int keyCode, KeyEvent event) {
                if(keyCode == KeyEvent.KEYCODE_DPAD_LEFT&&event.getAction() == KeyEvent.ACTION_DOWN){
                    int position = parentRecycle.getChildLayoutPosition(itemView);
                    if(position == 0){
                        callBack.setViewFocusAble();
                        return true;
                    }
                    return false;
                }
                return false;
            }
        }
        public class SetOnClick implements View.OnClickListener{

            @Override
            public void onClick(View view) {
                Bundle bundle = presenter.getParams(position);
                HomePageActivity homePageActivity = (HomePageActivity) context;
                Intent intent = new Intent();
                intent.putExtra("bundle",bundle);
                if(presenter.getVodType(position)){
                    intent.setClass(homePageActivity, FilmDetailsActivity.class);
                    context.startActivity(intent);
                }else if(presenter.getTelType(position)){
                    intent.setClass(homePageActivity, SitcomActivity.class);
                    context.startActivity(intent);
                }
            }
        }
    }
    class MovieWorldScrollListener extends RecyclerView.OnScrollListener {
        @Override
        public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
            if (newState == RecyclerView.SCROLL_STATE_IDLE) {

            }
        }
    }
}
